import enum

from sqlalchemy import Column, String, Integer, Numeric, DateTime, ForeignKey, Enum
from sqlalchemy import func
from sqlalchemy.orm import relationship, backref

from database import db
from .additional_item import AdditionalItem
from .currency import Currency
from .member import Member
from .product import Product


class NotifyMethod(enum.Enum):
    email = 'email'
    url = 'url'


class OrderStatus(enum.Enum):
    created = 'created'
    paid = 'paid'
    canceled = 'canceled'
    ended = 'ended'
    removed = 'removed'
    shipped = 'shipped'
    shipping = 'shipping'
    confirmed = 'confirmed'
    returned = 'returned'
    pay_error = 'pay_error'


class DeliveryMethod(enum.Enum):
    ticket = 'ticket'


class Order(db.Model):
    STATUS_CREATED = 'created'
    STATUS_WAIT_PAY = 'wait_pay'
    id = Column(Integer, primary_key=True, autoincrement=True)
    description = Column(String(200), doc='订单描述')
    remark = Column(String(800), nullable=True, doc='备注')
    member_id = Column(ForeignKey(Member.id), nullable=True, doc='用户ID')
    status = Column(String(30),nullable=False, doc='订单状态')
    fee = Column(Numeric(precision=15, scale=4), doc='手续费', default=0 ,nullable=False )
    real_fee = Column(Numeric(precision=15, scale=4), doc='实际手续费（由支付平台返回)', default=0 ,nullable=False)
    amount = Column(Numeric(precision=15, scale=4), doc='订单金额' ,nullable=False)
    real_amount = Column(Numeric(precision=15, scale=4), doc='用户实际支付金额' ,nullable=False)
    payment_method = Column(String(80), doc='支付方式', nullable=True)
    trade_id = Column(String(100), doc='交易编号', nullable=False)
    payment_seq = Column(String(120), doc='支付序列号,合并支付的时候需要', nullable=True)
    refunded_amount = Column(Numeric(15,4),doc='已退款金额',nullable=False,default=0.00)
    created_at = Column(DateTime, default=func.now(), doc='创建时间')
    updated_at = Column(DateTime, default=func.now(), doc='更新时间')
    expire_time = Column(DateTime, nullable=True, doc='过期时间')
    settle_time = Column(DateTime, nullable=True, doc='结算时间')
    currency_id = Column(ForeignKey(Currency.id), doc='币种',nullable=False)
    notify_method = Column(Enum(NotifyMethod), doc='通知方式')
    notify_info = Column(String(400), doc='通知信息', nullable=True)
    parent_id = Column(ForeignKey('order.id'))
    # delivery_method = Column(Enum(DeliveryMethod), default=DeliveryMethod.ticket)
    # delivery_info = Column(Text, nullable=True)
    #

    dealer_id = Column(ForeignKey(Member.id), nullable=False)

    # member = relationship(Member, backref='member_id')
    # parent = relationship('Order', backref=backref('children'), uselist=True)


class OrderDetail(db.Model):
    id = Column(Integer, primary_key=True, autoincrement=True)
    count = Column(Integer, default=1)
    amount = Column(Numeric(precision=15, scale=4),nullable=False)
    product_id = Column(ForeignKey(Product.id),nullable=False)
    order_id = Column(ForeignKey(Order.id),nullable=False)
    status = Column(String(20),nullable=False,default='normal')
    real_amount = Column(Numeric(precision=15, scale=4),nullable=False)
    delivery_method = Column(String)
    delivery_id = Column(String(120))

    order = relationship(Order, backref=backref("details"), uselist=True)

    product = relationship(Product)

class OrderDetailAdditional(db.Model):
    id = Column(Integer, primary_key=True, autoincrement=True)
    order_detail_id = Column(ForeignKey(OrderDetail.id),nullable=False)
    count = Column(Integer, default=1,nullable=False)
    amount = Column(Numeric(15, 4),nullable=False)
    additional_id = Column(ForeignKey(AdditionalItem.id),nullable=False)
    status = Column(String(20), nullable=False, default='normal')
    order_detail = relationship(OrderDetail, backref=backref('additionals'), uselist=True)
    additional = relationship(AdditionalItem)
